﻿//验证数据是否重复
var controller = this.lookupController();
var errInfo = "";
var gridItem = store.data.items;
var dataList = [];

controller.balance = controller.balance || {};
controller.budgetControl = controller.budgetControl || {};

if (record.crudStateWas == 'U') {
    //获取balance
    if (context.colIdx == 0 || context.colIdx == 2 || context.colIdx == 3) {
        if (gridItem.length > 1) {
            //获取当前record相同的值
            var sameObj = {};
            Ext.each(gridItem, function (record2, i) {
                if (record2.get('ProjectActivity') && record2.get('Company')) {
                    var key = Ext.String.format('{0}_{1}_{2}', record2.get('DescriptionOfSuppliesServices'), record2.get('ProjectActivity'), record2.get('Company'));

                    if (!Ext.Array.contains(dataList, key)) {
                        dataList.push(key);
                    } else {
                        controller.showMsgTips("The same data entry exists in Company Allocation. ");

                        record.data.CompanySharingRatio = 0;
                        record.data.Company = '';
                        record.commit();
                    }
                }
            });
        }

        for (var i = 0; i < store.data.items.length; i++) {
            var item = store.data.items[i];
            var pro = item.get('ProjectActivity');

            if (pro) {
                controller.getBalance(pro, item);

                var companyval = item.get('Company');
                if (companyval == "JLRC") {
                    item.data.Balance = controller.balance[pro] || 0;
                } else {
                    item.data.Balance = 'N';
                }
                item.data.budgetControl = controller.budgetControl[pro] || 'N';
                item.data.budgetControlId = controller.budgetControlId[pro];
                item.commit();
            }
        }

        //修改比例 相同 DescriptionOfSuppliesServices 和Project Activity的比例也要跟着变.
        if (context.colIdx == 3) {
            //Company = CJLR 并且 比例等于100 不允许编辑
            if (record.get('Company') == 'CJLR' && record.get('CompanySharingRatio') == 100) {
                record.set('CompanySharingRatio', 0);
                controller.showMsgTips("CJLR does not allow 100%. ");
            }
            //Company = JLRC 并且 比例等于0  也不允许编辑
            else if (record.get('Company') == 'JLRC' && record.get('CompanySharingRatio') == 0) {
                record.set('CompanySharingRatio', 0);
                controller.showMsgTips("JLRC does not allow 0%. ");
            }
            else {
                var expenseType = ref.ExpenseType.getValue();
                var purStore = expenseType == "4" ? ref.datagrid7.getStore() : ref.datagrid2.getStore();

                var r = purStore.findRecord('DescriptionOfSuppliesServices', record.get('DescriptionOfSuppliesServices'));

                if (r) {
                    store.each(function (item) {
                        if (item.get('DescriptionOfSuppliesServices') == record.get('DescriptionOfSuppliesServices') && item.get('ProjectActivity') == record.get('ProjectActivity')) {
                            if (item.get('Company') != record.get('Company')) {
                                item.data.CompanySharingRatio = 100 - record.get('CompanySharingRatio');
                                item.data.Cost = (r.data.TotalPrice * item.data.CompanySharingRatio) / 100;
                                item.commit();
                            }
                        }
                    });
                }
            }
        }
    }
}